<template>
  <div>
    <!-- 表格 -->
    <el-table ref="multipleTable"
              :data="tableData"
              v-loading='loading'
              size='mini'
              style="width: 100%">
      <el-table-column type="index"
                       :index="1"
                       label="序号"
                       align="center" />
      <el-table-column prop="comment"
                       label="评论内容" />
      <el-table-column label="评论人编号">
        <template slot-scope="scope">{{ scope.row.baseUser == null ? '': scope.row.baseUser.id }}</template>
      </el-table-column>

      <el-table-column prop="article_id"
                       label="文章编号" />
      <el-table-column prop="createdAt"
                       label="评论时间">
        <template slot-scope="scope">{{ moment(scope.row.createdAt).format("YYYY-MM-DD") }}</template>
      </el-table-column>

      <el-table-column prop="status"
                       label="评论状态">
        <template v-if="scope.row.status"
                  slot-scope="scope">
          <el-tag :type="(scope.row.status == null) ? '' :( (scope.row.status == '未审核') ? 'info' : (scope.row.status == '通过'? 'success' : 'danger') )">{{ scope.row.status == null ? '' :scope.row.status }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作"
                       align="center">
        <template v-if="scope.row.status!=null"
                  slot-scope="scope">
          <el-button size="mini"
                     type="warning"
                     @click="indeleteById(scope.row.id)">删除</el-button>
          <el-button type="success"
                     :disabled="scope.row.status !== '未审核'"
                     size='mini'
                     @click.stop="checkIsPass(scope.row, '通过')">审核</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getAllArticles, deleteCommentById, checkComment } from '@/api/comment'
import moment from 'moment'
export default {
  data() {
    return {
      tableData: [],
      multipleSelection: [],
      input: '', // 关键字
      batchArr: [],
      loading: false,
      moment
    };
  },
  created() {
    this.queryComments()
  },
  mounted() {

  },
  methods: {
    async queryComments() {
      this.loading = true
      const res = await getAllArticles();
      this.tableData = res.data
      this.loading = false
    },
    /**
     * 根据id删除
     */
    async indeleteById(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await deleteCommentById({ id })
        this.$message({
          type: 'success',
          message: res.message
        })
        this.queryComments()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    /**
     * 批量删除
     */
    async inbatchDelete() {
      const data = await deleteByIdArr(this.batchArr)
      console.log(data)
      if (data.status == 200 || data.status == '200') this.$message.success(data.message)
      else this.$message.error(data.message)
      this.inpageQuery()
    },

    /**
       * 审核是否通过
       */
    async checkIsPass(item, status) {
      const data = await checkComment({ id: item.id, status })
      if (data.status == 200 || data.status == '200') this.$message.success(data.message)
      else this.$message.error(data.message)
      this.queryComments()
    },
  }
};
</script>

<style scoped lang="less">
</style>
